<?php 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//SESSION STARTS

session_start();
$sid = session_id();
////////////////////////////////////////////////////////////////////////////////////////////////////////////

//CONNECT TO MYSQL

$con = mysql_connect('localhost:3306', 'root', 'vamshi');
if(!$con) {
die('Could not connect: ' . mysql_error());
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

//CREATE DATABASE IF DOES NOT EXIST

$sql = 'CREATE DATABASE if not exists CollabSum';
if (mysql_query($sql, $con)) {
    //echo "Database CollSum created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

// CREATE TABLES IN DATABASE COLLSUM

mysql_select_db("CollabSum", $con);
$tbl1 = "CREATE TABLE if not exists summary(sid varchar(75) , url varchar(250),summ varchar(1000),rank int, PRIMARY KEY(sid, url))";
mysql_query($tbl1);
$content = $_REQUEST["p"];
$tbl2 = "CREATE TABLE if not exists sentence(sent varchar(500), frequency int)";
mysql_query($tbl2);
list($url,$txt) = split('collsum', $content);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//INSERT AND UPDATE DATABASE SUMMARY INTO DATABASE

$dup = mysql_query("SELECT summ from summary where url = '$url' and sid = '$sid'");
if(mysql_num_rows($dup) == 0) {
	$add = "INSERT INTO summary(sid,url,summ) values('$sid', '$url', '$txt')";
	mysql_query($add);
}
else if(mysql_num_rows($dup)!= 0) {
	while($row1 = mysql_fetch_array($dup)) {		
		$sum1 = $row1['summ'];
		$sum1 = $sum1 . $txt ;
		mysql_query("UPDATE summary SET summ = '$sum1' where sid = '$sid' and url = '$url'");	
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//SPLIT SUMMARY INTO SENTENCES AND STORE THE SNETENCES AND FREQUENCY INTO DATABASE

$words = preg_split('/\./', $txt, -1, PREG_SPLIT_NO_EMPTY);
foreach($words as $word) {	
	$flag = mysql_query("select frequency from sentence where sent = '$word'");
	if(mysql_num_rows($flag) == 0) {
		mysql_query("INSERT INTO sentence values('$word', 1)");
	}
	else if(mysql_num_rows($flag)!= 0) {
		while($row = mysql_fetch_array($flag)) {
			$freq = $row['frequency'] + 1;
			mysql_query("UPDATE sentence SET frequency = '$freq' WHERE sent = '$word' ");
		}
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// RANK THE SUMMARY 

$summary = "SELECT summ from summary where url ='$url'";
$flag_sum = mysql_query($summary);
if(mysql_num_rows($flag_sum)!=0) {		
	while($sum = mysql_fetch_array($flag_sum)) {
		$freq1 = 0;
		$each = preg_split('/\./', $sum['summ'], -1, PREG_SPLIT_NO_EMPTY);
		foreach($each as $sentence){
			echo "sentence: $sentence\n";
			$frequency = "SELECT frequency from sentence where sent = '$sentence'";
			$flag_freq = mysql_query($frequency);
			if(mysql_num_rows($flag_freq)!=0) {		
				while($rank = mysql_fetch_array($flag_freq)) {
					$freq1 = $freq1 + $rank['frequency'];
				}
			}
		}
		mysql_query("UPDATE summary SET rank = '$freq1' WHERE url = '$url' ");
	}
}
?>


